package com.zqwh.yj.sdk.utils;

import android.graphics.Bitmap;
import android.media.ThumbnailUtils;

import java.util.Arrays;


/* 
* pHash-like image hash.  
* Author: Elliot Shepherd (elliot@jarofworms.com 
* Based On: http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html 
*/
public class ImagePHash {
    public static String getHash(Bitmap bitmapOriginal){
        // 缩小尺寸
        Bitmap bitmap1 = ThumbnailUtils.extractThumbnail(bitmapOriginal, 16, 16);
        // 简化色彩
        Bitmap bitmap2 = convertGreyImg(bitmap1);
        // 计算平均值
        long avg = getAvg(bitmap2);
        // 比较像素的灰度
        String binary = getBinary(bitmap2, avg);
        // 计算哈希值
        String hash = binaryString2hexString(binary);
        return hash;
    }

	public static Bitmap convertGreyImg(Bitmap img) {
		int width = img.getWidth();         //获取位图的宽
		int height = img.getHeight();       //获取位图的高

		int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组

		img.getPixels(pixels, 0, width, 0, 0, width, height);
		int alpha = 0xFF << 24;
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				int original = pixels[width * i + j];
				int red = ((original & 0x00FF0000) >> 16);
				int green = ((original & 0x0000FF00) >> 8);
				int blue = (original & 0x000000FF);

				int grey = (int) ((float) red * 0.3 + (float) green * 0.59 + (float) blue * 0.11);
				grey = alpha | (grey << 16) | (grey << 8) | grey;
				pixels[width * i + j] = grey;
			}
		}
		Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
		result.setPixels(pixels, 0, width, 0, 0, width, height);
		return result;
	}

    public static long getAvg(Bitmap img) {
        int width = img.getWidth();
        int height = img.getHeight();
        int[] pixels = new int[width * height];
        img.getPixels(pixels, 0, width, 0, 0, width, height);
        LogUtils.logout("pix length : "+pixels.length+ ", pix[] : "+ Arrays.toString(pixels));
        long avgPixel = 0;
        for (int pixel : pixels) {
            avgPixel += pixel;
        }
        return avgPixel / pixels.length;
    }

    public static String getBinary(Bitmap img, long average) {
        StringBuilder sb = new StringBuilder();

        int width = img.getWidth();
        int height = img.getHeight();
        int[] pixels = new int[width * height];

        img.getPixels(pixels, 0, width, 0, 0, width, height);
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int original = pixels[width * i + j];
                if (original >= average) {
                    pixels[width * i + j] = 1;
                } else {
                    pixels[width * i + j] = 0;
                }
                sb.append(pixels[width * i + j]);
            }
        }
        return sb.toString();
    }

    public static String binaryString2hexString(String bString) {
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuilder sb = new StringBuilder();
        int iTmp;
        for (int i = 0; i < bString.length(); i += 4) {
            iTmp = 0;
            for (int j = 0; j < 4; j++) {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            sb.append(Integer.toHexString(iTmp));
        }
        return sb.toString();
    }

    public static int diff(String s1, String s2) {
        if (StringUtils.areNotEmpty(s1, s2)) {
            char[] s1s = s1.toCharArray();
            char[] s2s = s2.toCharArray();
            int diffNum = 0;
            for (int i = 0; i < s1s.length; i++) {
                if (s1s[i] != s2s[i]) {
                    diffNum++;
                }
            }
            return diffNum;
        }else {
            return 16;
        }
    }
}